package com.baidu.health.controller;

import com.baidu.health.mapper.TbCaseMapper;
import com.baidu.health.pojo.CaseContentVo;
import com.baidu.health.pojo.TbCase;
import com.baidu.health.pojo.TbCaseContent;
import com.baidu.health.pojo.TbCaseType;
import com.baidu.health.service.TbCaseContentService;
import com.baidu.health.service.TbCaseService;
import com.baidu.health.service.TbCaseTypeService;
import com.baidu.health.util.AuthCodeUtil;
import com.baidu.health.util.resFaqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(tags = "方案中心")
@RestController
@RequestMapping("a")
public class TestController {
    @Autowired
    TbCaseService tbCaseService;
    @Autowired
    TbCaseContentService tbCaseContentService;
    @Autowired
    TbCaseTypeService tbCaseTypeService;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    TbCaseMapper tbCaseMapper;

    //数据抓取
    @PostMapping("jsoup")
    private resFaqVo jsoup() throws IOException {
        Document doc = Jsoup.connect("https://search.jd.com/Search?keyword=%E5%BF%83%E8%84%8F%E7%97%85&enc=utf-8&wq=%E5%BF%83%E8%84%8F%E7%97%85&pvid=ee2e1b3ea3b34220a04e598fa13b703a").get();
        Element j_goodsList=doc.getElementById("J_goodsList");
        
        Elements elements=j_goodsList.getElementsByClass("gl-i-wrap");
        elements.forEach(e ->{
            Elements pNameclass = e.getElementsByClass("p-name p-name-type-2");
            String namess=pNameclass.first().text();
            System.out.println("----------"+namess);

//            Elements ppriceclass = e.getElementsByClass("hot_item_a2");
//            String pricess=pNameclass.first().text();
//            System.out.println("==========="+pricess);

//            List a=new ArrayList();
//            a.add(namess);
            TbCase tt=new TbCase();
            tt.setCaseName(namess);
            tbCaseMapper.insert(tt);

        });



        return new resFaqVo(200, "抓取成功", null);
    }


    //创建es索引
    @RequestMapping("indexES")
    private String indesES(){
        elasticsearchRestTemplate.indexOps(TbCase.class).create();
        return "成功";
    }

    //删除es索引
    @RequestMapping("deleteES")
    private String deleteES(){
        elasticsearchRestTemplate.indexOps(TbCase.class).delete();
        return "删除成功";
    }

    /**
     * es分页
     * @return
     */
    @PostMapping("caseEsPage")
    private resFaqVo caseEsPage(@RequestBody TbCase tbCasees){
        return tbCaseService.caseEsPage(tbCasees);
    }

   //-----------------------------------------
   //运动方案列表
    @ApiOperation("运动方案列表")
    @PostMapping("lista")
    private resFaqVo lista(){
       return tbCaseContentService.lista();
    }

    /**
     * 运动方案分页
     * @param tbCaseContentpage
     * @return
     */
    @ApiOperation("运动方案分页")
    @PostMapping("ydpage")
    private resFaqVo ydpage(@RequestBody TbCaseContent tbCaseContentpage){
        return tbCaseContentService.ydpage(tbCaseContentpage);
    }

    //食谱方案列表
    @ApiOperation("食谱方案列表")
    @PostMapping("listb")
    private resFaqVo listb(){
        return tbCaseContentService.listb();
    }

    /**
     * 食谱方案分页
     * @param tbCaseContentpage
     * @return
     */
    @ApiOperation("食谱方案分页")
    @PostMapping("sppage")
    private resFaqVo sppage(@RequestBody TbCaseContent tbCaseContentpage){
        return tbCaseContentService.sppage(tbCaseContentpage);
    }
    //设备方案列表
    @ApiOperation("设备方案列表")
    @PostMapping("listc")
    private resFaqVo listc(){
        return tbCaseContentService.listc();
    }

    /**
     * 设备方案分页
     * @param tbCaseContentpage
     * @return
     */
    @ApiOperation("设备方案分页")
    @PostMapping("sbpage")
    private resFaqVo sbpage(@RequestBody TbCaseContent tbCaseContentpage){
        return tbCaseContentService.sbpage(tbCaseContentpage);
    }
    //学习方案列表
    @ApiOperation("学习方案列表")
    @PostMapping("listd")
    private resFaqVo listd(){
        return tbCaseContentService.listd();
    }

    /**
     * 学习方案分页
     * @param tbCaseContentpage
     * @return
     */
    @ApiOperation("学习方案分页")
    @PostMapping("xxpage")
    private resFaqVo xxpage(@RequestBody TbCaseContent tbCaseContentpage){
        return tbCaseContentService.xxpage(tbCaseContentpage);
    }
   @PostMapping("caseContentList")
   private List<TbCaseContent> caseContentList(){
      return tbCaseContentService.selectlist();
   }
   //方案内容添加/修改（传id）
   @PostMapping("caseContentAdd")
   private resFaqVo caseContentAdd(@RequestBody TbCaseContent tbCaseContent){
       return tbCaseContentService.caseContentAdd(tbCaseContent);
   }
   //方案查询
   @PostMapping("caseList")
   private resFaqVo caseList(){
        return tbCaseService.caseList();
   }
   //方案添加/修改（传cid）
   @ApiOperation("方案添加")
   @PostMapping("caseAdd")
   private resFaqVo caseadd(@RequestBody CaseContentVo caseContentVo){
        return tbCaseService.caseadd(caseContentVo);
   }
   //方案删除
   @ApiOperation("逻辑删除")
   @PostMapping("caseDelete")
   private resFaqVo caseDelete(@RequestBody TbCase tbCasea){
       return tbCaseService.caseDelete(tbCasea);
   }
    //方案内容删除
    @ApiOperation("逻辑删除")
    @PostMapping("caseContentDelete")
    private resFaqVo caseDelete(@RequestBody TbCaseContent tbCaseContentd){
        return tbCaseContentService.caseContentDelete(tbCaseContentd);
    }

   //方案类型列表
   @PostMapping("caseType")
   private resFaqVo caseType(){
       return tbCaseTypeService.caseType();
   }

   //分页
   @ApiOperation("方案分页")
   @PostMapping("casePage")
   private resFaqVo casePage(@RequestBody TbCase tbCase){ return tbCaseService.casePage(tbCase); }


   //----------------------------------------------有评测报告

   //根据方案id查询方案内容
   @RequestMapping("caseContentBycaseid")
   private resFaqVo caseContentBycaseid(@RequestBody TbCaseContent tbCaseContentid){
       System.out.println(tbCaseContentid.toString());
       return tbCaseContentService.caseContentBycaseid(tbCaseContentid);
   }
   //根据方案内容id查询
   @RequestMapping("caseContentById")
   private resFaqVo caseContentById(@RequestBody TbCaseContent tbCaseContentid){
       return tbCaseContentService.caseContentById(tbCaseContentid);
   }

    @RequestMapping("yzm")
    private void yzm(){
        String yzm= AuthCodeUtil.getCode("0-9", 6);
        //使用redis确保验证码只在1分钟内有效（5分）
        //stringRedisTemplate.opsForValue().set("yzm", yzm,1, TimeUnit.MINUTES);
        //AuthCodeUtil.sendMsgToPhone(yzm,"15150774639");
    }
//
//   @PostMapping("time")
//   private void sj(){
//       Date data=new Date();
//       SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
//       String a=simpleDateFormat.format(data );
//       System.out.println("---"+a);
//
//       Calendar cl=Calendar.getInstance();
//       cl.setTime(data);
//       cl.set(Calendar.HOUR_OF_DAY, 0);
//       cl.set(Calendar.MINUTE, 0);
//       cl.set(Calendar.SECOND, 0);
//
//       cl.add(Calendar.DATE, 5);
//       String b=simpleDateFormat.format(cl.getTime());
//       System.out.println("+++"+b);
//
//   }

//dhgsjfshjshdsjhf
   //sssssss
}
